//
//  MessageViewController.m
//  XMPP
//
//  Created by  江志磊 on 15-3-6.
//  Copyright (c) 2015年 江 志磊. All rights reserved.
//

#import "MessageViewController.h"
#import "QFXmppManager.h"
#import "MessageModel.h"//消息对象，存放消息
#import "NSData+Base64.h"//xmpp中图片转化成NSData无法直接发送,需要对图片进行base64转码(将图片转换成可读性较差的字符串数据),base64常用于对图片进行加密转换;

@interface MessageViewController ()
{
    NSMutableArray *_messageArray;//用于存放登录账号收到的消息
}
@end

@implementation MessageViewController



- (void)viewDidLoad
{
    [super viewDidLoad];
    _messageArray = [[NSMutableArray alloc] init];
    //接收别人给登录账号发送过来的消息
    [[QFXmppManager shareInstance]registerForMessage:^(MessageModel *mm) {
        //MessageModel中存放的是别人给登录账号发送的消息实体
        [_messageArray addObject:mm];
        NSLog(@"msg==%@ from==%@ to==%@",mm.msg,mm.from,mm.to);
        //显示收到的消息
        [self.tableView reloadData];
    }];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell1"];
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"send" style:UIBarButtonItemStylePlain target:self action:@selector(sendMessage)];
    self.navigationItem.rightBarButtonItem = item;
}
 //给指定好友发送消息
- (void)sendMessage{
    [self sendText];
    [self sendImage];
}
//发送文字
- (void)sendText{
    if (_currentModel) {
        //sendMessage 要发送的文字; type 消息的类型; toUser 要将消息发送给谁
        [[QFXmppManager shareInstance] sendMessage:@"hello! " withType:kMsgText toUser:_currentModel withCompletion:^(BOOL ret) {
           //发送完成后，调用此block
            if (ret==YES) {
                NSLog(@"发送成功!");
            }else{
                NSLog(@"发送失败!");
            }
        }];
    }else{
        NSLog(@"没有指定好友");
    }
}
//发送图片
- (void)sendImage{
    UIImage *image = [UIImage imageNamed:@"online.png"];
    //转化成NSData
    NSData *data = UIImagePNGRepresentation(image);
    //将图片转换成base64加密的字符串
    NSString *imageString = [data base64EncodedString];
    //发送图片
    //kMsgImage 发送的类型为图片
    [[QFXmppManager shareInstance] sendMessage:imageString withType:kMsgImage toUser:_currentModel withCompletion:^(BOOL ret) {
        if (ret ==YES) {
            NSLog(@"图片发送成功!");
        }else{
            NSLog(@"图片发送失败!");
        }
    }];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return _messageArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1" forIndexPath:indexPath];
    MessageModel *msg = _messageArray[indexPath.row];
    //显示发送过来的消息
    cell.textLabel.text = msg.msg;
    if (msg.image) {
        //如果有图片，则显示
        cell.imageView.image =msg.image;
    }
    return cell;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
